package com.mlan.userap.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.kongzue.baseokhttp.util.JsonList;
import com.kongzue.baseokhttp.util.JsonMap;
import com.mlan.userap.AppConfig;
import com.mlan.userap.R;
import com.mlan.userap.dialog.LoadingDialog;

import org.greenrobot.eventbus.EventBus;

public class BaseActivity extends AppCompatActivity {
    private LoadingDialog dialogLoading = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
    }

    public void loadingShow() {
        dialogLoading = new LoadingDialog(this);
        dialogLoading.show();
    }

    public void loadingClose() {
        if (dialogLoading != null) {
            dialogLoading.dismiss();
            dialogLoading = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void showToast(String content) {
        Toast.makeText(this, content, Toast.LENGTH_SHORT).show();
    }

    //跳转到打电话页面
    public void startCallActivity(String json) {
        JsonMap jsonMap = new JsonMap(json);
        String groupId = "";
        String sdkAppId = "";
        String userSig = "";
        String roomId = "";
        int stype = 0;
        if (!jsonMap.getString("groupId").isEmpty()) {
            groupId = jsonMap.getString("groupId");
            stype = jsonMap.getInt("stype");
            JsonMap enterInfo = jsonMap.getJsonMap("enterInfo");
            sdkAppId = enterInfo.getString("sdkAppId");
            userSig = enterInfo.getString("userSig");
            roomId = enterInfo.getString("roomId");
        }
        JsonMap dataJson = jsonMap.getJsonMap("data");
        if (!dataJson.getString("groupId").isEmpty()){
            groupId = dataJson.getString("groupId");
            stype = dataJson.getInt("stype");
            JsonMap enterInfo = dataJson.getJsonMap("enterInfo");
            sdkAppId = enterInfo.getString("sdkAppId");
            userSig = enterInfo.getString("userSig");
            roomId = enterInfo.getString("roomId");
        }
        if (groupId.isEmpty() && sdkAppId.isEmpty() && userSig.isEmpty() && roomId.isEmpty()) {
            return;
        }
        JsonMap createUser = jsonMap.getJsonMap("createUser");
        JsonList userList = jsonMap.getList("userList");
        if (createUser.getLong("userId") == AppConfig.userId) {
            return;
        }
        //判断是否是给我打的电话
        for (int i = 0; i < userList.size(); i++) {
            long jsonValueItem = userList.getJsonMap(i).getLong("userId");
            if (jsonValueItem == AppConfig.userId) {
                Intent intent = new Intent(this, CallActivity.class);
                intent.putExtra("eventGroupInfo", json);
                intent.putExtra("callType", 1);
                intent.putExtra("stype", stype);
                startActivity(intent);
            }
        }
    }
}
//场景一
// A 打电话 给 B
// B 看到通知 选择 接电话 还是 拒绝接
// B 同意则 A 拿到B的同意
// B 拒绝  A则提示对方挂断
//场景二
// A 打电话同时  过了一两秒 看到B没接电话 A挂断 B收到通知挂断
// 场景三
// A B 在通话过程中  A 或B 挂断 则对方收到通知